package com.example.tabbar.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.R
import com.example.tabbar.databinding.ItemSamplingDetailsBinding

import com.junray.usermangement.room.bean.SamplingData

class DetailsDataAdapter: RecyclerView.Adapter<DetailsDataAdapter.ViewHolder>() {
    private var items: MutableList<SamplingData> = ArrayList()
    private var selectedPositions: MutableSet<Int> = HashSet() // 用于跟踪选中项的位置
    private var selectAllListener: ((Boolean) -> Unit)? = null // 添加全选状态监听器
    fun setOnItemClickListener(listener: (Boolean) -> Unit) {
        this.selectAllListener = listener
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemSamplingDetailsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]

        holder.serialNum.text = (position + 1).toString()
        val split = item.startTime.split(" ")
        holder.yearMonth.text = split[0]
        holder.time.text = split[1]
        holder.group.text = "群组：" + item.groupName
        holder.location.text = "区域：" + item.locationName
        holder.flowRate.text = "流速：" + item.flow.toString() + "L/min"
        if(item.result) {
            holder.result.setImageResource(R.drawable.green_dot)
        } else {
            holder.result.setImageResource(R.drawable.red_dot)
        }


        // 设置选中状态
        holder.checkBox.isChecked = selectedPositions.contains(position)

        // 设置选择状态改变监听器
        holder.checkBox.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                selectedPositions.add(position)
            } else {
                selectedPositions.remove(position)
            }

            // 检查是否需要更新全选按钮状态
            notifySelectAllStateChanged()
        }
    }

    // 全选/取消全选方法
    fun selectAll(select: Boolean) {
        selectedPositions.clear()
        if (select) {
            // 选中所有项
            for (i in 0 until items.size) {
                selectedPositions.add(i)
            }
        }
        notifyDataSetChanged()
        selectAllListener?.invoke(select)
    }

    // 检查是否全选
    private fun isAllSelected(): Boolean {
        return selectedPositions.size == items.size && items.isNotEmpty()
    }
    // 通知全选状态改变
    private fun notifySelectAllStateChanged() {
        val isAllSelected = isAllSelected()
        selectAllListener?.invoke(isAllSelected)
    }

    override fun getItemCount(): Int {
        return items.size ?: 0
    }

    fun setItems(items: List<SamplingData>?) {
        this.items.clear()
        if (items != null) {
            this.items.addAll( items)
            notifyDataSetChanged()
        }
    }


    class ViewHolder(binding: ItemSamplingDetailsBinding)
        : RecyclerView.ViewHolder(binding.root) {
        var checkBox: CheckBox = binding.isSelect
        var serialNum = binding.serialNumber
        var userName :TextView = binding.userName
        var yearMonth:TextView = binding.yearMonth
        var time:TextView = binding.time
        var location :TextView = binding.location
        var group :TextView = binding.group
        var samplingLoc :TextView = binding.samplingLoc
        var flowRate :TextView = binding.flowRate
        var value03 :TextView = binding.valueZeroThree
        var value5 :TextView = binding.valueFive
        var result :ImageView= binding.results
    }

}